home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / ServerSocket.java < prev    next >
Text File  |  1998-09-22  |  10KB  |  299 lines

  1. /*
  2.  * @(#)ServerSocket.java    1.28 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.net;
  16.  
  17. import java.io.IOException;
  18. import java.io.FileDescriptor;
  19.  
  20. /**
  21.  * This class implements server sockets. A server socket waits for 
  22.  * requests to come in over the network. It performs some operation 
  23.  * based on that request, and then possibly returns a result to the requester.
  24.  * <p>
  25.  * The actual work of the server socket is performed by an instance 
  26.  * of the <code>SocketImpl</code> class. An application can 
  27.  * change the socket factory that creates the socket 
  28.  * implementation to configure itself to create sockets 
  29.  * appropriate to the local firewall. 
  30.  *
  31.  * @author  unascribed
  32.  * @version 1.28, 07/01/98
  33.  * @see     java.net.SocketImpl
  34.  * @see     java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
  35.  * @since   JDK1.0
  36.  */
  37. public 
  38. class ServerSocket {
  39.     /**
  40.      * The implementation of this Socket.
  41.      */
  42.     private SocketImpl impl;
  43.  
  44.     /**
  45.      * Creates an unconnected server socket. Note: this method
  46.      * should not be public.
  47.      * @exception IOException IO error when opening the socket.
  48.      */
  49.     private ServerSocket() throws IOException {
  50.     impl = (factory != null) ? factory.createSocketImpl() : 
  51.         new PlainSocketImpl();
  52.     }
  53.  
  54.     /**
  55.      * Creates a server socket on a specified port. A port of 
  56.      * <code>0</code> creates a socket on any free port. 
  57.      * <p>
  58.      * The maximum queue length for incoming connection indications (a 
  59.      * request to connect) is set to <code>50</code>. If a connection 
  60.      * indication arrives when the queue is full, the connection is refused.
  61.      * <p>
  62.      * If the application has specified a server socket factory, that 
  63.      * factory's <code>createSocketImpl</code> method is called to create 
  64.      * the actual socket implementation. Otherwise a "plain" socket is created.
  65.      *
  66.      * @param      port  the port number, or <code>0</code> to use any
  67.      *                   free port.
  68.      * @exception  IOException  if an I/O error occurs when opening the socket.
  69.      * @see        java.net.SocketImpl
  70.      * @see        java.net.SocketImplFactory#createSocketImpl()
  71.      * @see        java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
  72.      * @since      JDK1.0
  73.      */
  74.     public ServerSocket(int port) throws IOException {
  75.     this(port, 50, null);
  76.     }
  77.  
  78.     /**
  79.      * Creates a server socket and binds it to the specified local port 
  80.      * number. A port number of <code>0</code> creates a socket on any 
  81.      * free port. 
  82.      * <p>
  83.      * The maximum queue length for incoming connection indications (a 
  84.      * request to connect) is set to the <code>count</code> parameter. If 
  85.      * a connection indication arrives when the queue is full, the 
  86.      * connection is refused. 
  87.      * <p>
  88.      * If the application has specified a server socket factory, that 
  89.      * factory's <code>createSocketImpl</code> method is called to create 
  90.      * the actual socket implementation. Otherwise a "plain" socket is created.
  91.      *
  92.      * @param      port     the specified port, or <code>0</code> to use
  93.      *                      any free port.
  94.      * @param      backlog  the maximum length of the queue.
  95.      * @exception  IOException  if an I/O error occurs when opening the socket.
  96.      * @see        java.net.SocketImpl
  97.      * @see        java.net.SocketImplFactory#createSocketImpl()
  98.      * @see        java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
  99.      * @since      JDK1.0
  100.      */
  101.     public ServerSocket(int port, int backlog) throws IOException {
  102.     this(port, backlog, null);
  103.     }
  104.  
  105.     /** 
  106.      * Create a server with the specified port, listen backlog, and 
  107.      * local IP address to bind to.  The <i>bindAddr</i> argument
  108.      * can be used on a multi-homed host for a ServerSocket that
  109.      * will only accept connect requests to one of its addresses.
  110.      * If <i>bindAddr</i> is null, it will default accepting
  111.      * connections on any/all local addresses.
  112.      * The port must be between 0 and 65535, inclusive.
  113.      * <P>
  114.      * @param port the local TCP port
  115.      * @param backlog the listen backlog
  116.      * @param bindAddr the local InetAddress the server will bind to
  117.      * @see SocketConstants
  118.      * @see SocketOption
  119.      * @see SocketImpl
  120.      * @see   JDK1.1
  121.      */
  122.     public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException {
  123.         this();
  124.  
  125.     if (port < 0 || port > 0xFFFF)
  126.         throw new IllegalArgumentException(
  127.                "Port value out of range: " + port);
  128.     try {
  129.         SecurityManager security = System.getSecurityManager();
  130.         if (security != null) {
  131.         security.checkListen(port);
  132.         }
  133.  
  134.         impl.create(true); // a stream socket
  135.         if (bindAddr == null)
  136.         bindAddr = InetAddress.anyLocalAddress;    
  137.  
  138.         impl.bind(bindAddr, port);
  139.         impl.listen(backlog);
  140.  
  141.     } catch(SecurityException e) {
  142.         impl.close();
  143.         throw e;
  144.     } catch(IOException e) {
  145.         impl.close();
  146.         throw e;
  147.     }
  148.     }
  149.  
  150.     /**
  151.      * Returns the local address of this server socket.
  152.      *
  153.      * @return  the address to which this socket is connected,
  154.      *          or <code>null</code> if the socket is not yet connected.
  155.      * @since   JDK1.0
  156.      */
  157.     public InetAddress getInetAddress() {
  158.     return impl.getInetAddress();
  159.     }
  160.  
  161.     /**
  162.      * Returns the port on which this socket is listening.
  163.      *
  164.      * @return  the port number to which this socket is listening.
  165.      * @since   JDK1.0
  166.      */
  167.     public int getLocalPort() {
  168.     return impl.getLocalPort();
  169.     }
  170.  
  171.     /**
  172.      * Listens for a connection to be made to this socket and accepts 
  173.      * it. The method blocks until a connection is made. 
  174.      *
  175.      * @exception  IOException  if an I/O error occurs when waiting for a
  176.      *               connection.
  177.      * @since      JDK1.0
  178.      */
  179.     public Socket accept() throws IOException {
  180.     Socket s = new Socket();
  181.     implAccept(s);
  182.     return s;
  183.     }
  184.  
  185.     /**
  186.      * Subclasses of ServerSocket use this method to override accept()
  187.      * to return their own subclass of socket.  So a FooServerSocket
  188.      * will typically hand this method an <i>empty</i> FooSocket().  On
  189.      * return from implAccept the FooSocket will be connected to a client.
  190.      *
  191.      * @since   JDk1.1
  192.      */
  193.     protected final void implAccept(Socket s) throws IOException {
  194.     try {
  195.         //s.impl.create(true);
  196.         s.impl.address = new InetAddress();
  197.         s.impl.fd = new FileDescriptor();
  198.         impl.accept(s.impl);
  199.         
  200.         SecurityManager security = System.getSecurityManager();
  201.         if (security != null) {
  202.         security.checkAccept(s.getInetAddress().getHostAddress(),
  203.                      s.getPort());
  204.         }
  205.     } catch (IOException e) {
  206.         s.close();
  207.         throw e;
  208.     } catch (SecurityException e) {
  209.         s.close();
  210.         throw e;
  211.     }
  212.     }
  213.  
  214.     /**
  215.      * Closes this socket. 
  216.      *
  217.      * @exception  IOException  if an I/O error occurs when closing the socket.
  218.      * @since      JDK1.0
  219.      */
  220.     public void close() throws IOException {
  221.     impl.close();
  222.     }
  223.  
  224.     /** Enable/disable SO_TIMEOUT with the specified timeout, in
  225.      *  milliseconds.  With this option set to a non-zero timeout,
  226.      *  a call to accept() for this ServerSocket
  227.      *  will block for only this amount of time.  If the timeout expires,
  228.      *  a <B>java.io.InterruptedIOException</B> is raised, though the
  229.      *  ServerSocket is still valid.  The option <B>must</B> be enabled
  230.      *  prior to entering the blocking operation to have effect.  The 
  231.      *  timeout must be > 0.
  232.      *  A timeout of zero is interpreted as an infinite timeout.  
  233.      *
  234.      * @since   JDK1.1
  235.      */
  236.     public synchronized void setSoTimeout(int timeout) throws SocketException {
  237.     impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
  238.     }
  239.  
  240.     /** Retrive setting for SO_TIMEOUT.  0 returns implies that the
  241.      *  option is disabled (i.e., timeout of infinity).
  242.      *
  243.      * @since   JDK1.1
  244.      */
  245.     public synchronized int getSoTimeout() throws IOException {
  246.     Object o = impl.getOption(SocketOptions.SO_TIMEOUT);
  247.     /* extra type safety */
  248.     if (o instanceof Integer) {
  249.         return ((Integer) o).intValue();
  250.     } else {
  251.         return 0;
  252.     }
  253.     }
  254.  
  255.     /**
  256.      * Returns the implementation address and implementation port of 
  257.      * this socket as a <code>String</code>.
  258.      *
  259.      * @return  a string representation of this socket.
  260.      * @since   JDK1.0
  261.      */
  262.     public String toString() {
  263.     return "ServerSocket[addr=" + impl.getInetAddress() + 
  264.         ",port=" + impl.getPort() + 
  265.         ",localport=" + impl.getLocalPort()  + "]";
  266.     }
  267.  
  268.     /**
  269.      * The factory for all server sockets.
  270.      */
  271.     private static SocketImplFactory factory;
  272.  
  273.     /**
  274.      * Sets the server socket implementation factory for the 
  275.      * application. The factory can be specified only once. 
  276.      * <p>
  277.      * When an application creates a new server socket, the socket 
  278.      * implementation factory's <code>createSocketImpl</code> method is 
  279.      * called to create the actual socket implementation. 
  280.      *
  281.      * @param      fac   the desired factory.
  282.      * @exception  IOException  if an I/O error occurs when setting the
  283.      *               socket factory.
  284.      * @exception  SocketException  if the factory has already been defined.
  285.      * @see        java.net.SocketImplFactory#createSocketImpl()
  286.      * @since      JDK1.0
  287.      */
  288.     public static synchronized void setSocketFactory(SocketImplFactory fac) throws IOException {
  289.     if (factory != null) {
  290.         throw new SocketException("factory already defined");
  291.     }
  292.     SecurityManager security = System.getSecurityManager();
  293.     if (security != null) {
  294.         security.checkSetFactory();
  295.     }
  296.     factory = fac;
  297.     }
  298. }
  299.